package cn.life.business.warehouse.service.impl;

import cn.life.business.warehouse.dao.WarehouseDao;
import cn.life.business.warehouse.entity.po.Warehouse;
import cn.life.business.warehouse.entity.po.WarehouseDetails;
import cn.life.business.warehouse.entity.vo.WarehouseDetailsVo;
import cn.life.business.warehouse.entity.vo.WarehouseVo;
import cn.life.business.warehouse.service.WarehouseService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-07-31 14:38:52
 */
@Service
@Transactional
public class WarehouseServiceImpl implements WarehouseService {
    @Autowired
    private WarehouseDao warehouseDao;


    @Override
    public CMessage addWarehouse(Warehouse warehouse) {
        Long warehouseId=warehouseDao.addWarehouse(warehouse);
        if (warehouseId!=null){
         warehouse.setWarehouseId(warehouseId);
         if (warehouseDao.addWarehouseDetails(warehouse)==1){
             return CMessage.addSuccess();
         }
        }
        return CMessage.addFail();
    }

    @Override
    public CMessage deletewarehouse(Warehouse warehouse) {
        int n=warehouseDao.deleteWarehouse(warehouse);
        if (n!=0){
            warehouseDao.deleteWarehouseDetails(warehouse);
            return CMessage.removeSuccess();
        }
        return CMessage.removeFail();
    }


    @Override
    public CMessage updateWarehouse(Warehouse warehouse) {
        return warehouseDao.updateWarehouse(warehouse)== 1 ? CMessage.modifySuccess() : CMessage.modifyFail();
    }

    @Override
    public CMessage updateWarehouseDetails(WarehouseDetails warehouseDetails) {
        return warehouseDao.updateWarehouseDetails(warehouseDetails)== 1 ? CMessage.modifySuccess() : CMessage.modifyFail();
    }

    @Override
    public CMessage selectWarehouse(WarehouseVo warehouseVo) {
        Pagination<WarehouseVo> list = warehouseDao.select(warehouseVo);
        return CMessage.successByData(list);
    }

    @Override
    public CMessage queryWarehouseDetail(WarehouseVo warehouseVo) {
        warehouseVo = warehouseDao.getWarehouse(warehouseVo);
        if(warehouseVo == null){
            return CMessage.success();
        }
        List<WarehouseDetailsVo> list=warehouseDao.queryWarehouseDetailList(warehouseVo);
        warehouseVo.setList(list);
        return CMessage.successByData(warehouseVo);
    }

    @Override
    public CMessage getWarehouse(WarehouseVo warehouseVo) {
        warehouseVo=warehouseDao.getWarehouse(warehouseVo);
        warehouseVo.setList(warehouseDao.getWarehouseDetails(warehouseVo));
        return CMessage.successByData(warehouseVo);
    }

    @Override
    public CMessage getWarehouseByState(WarehouseVo warehouseVo) {
        Pagination<WarehouseVo> list=warehouseDao.getWarehouseByState(warehouseVo);
        return CMessage.successByData(list);
    }
}
